package com.qkst.website.backstage.controller;


import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.qkst.website.backstage.bean.CategoryBean;
import com.qkst.website.backstage.bean.CategoryRelAttrBean;
import com.qkst.website.backstage.bean.ResultBean;
import com.qkst.website.backstage.service.CategoryService;
import com.qkst.website.common.core.BaseController;

/**
 * @author lh
 * @description 产品类别控制器
 * @date 2017-05-03
 * @email ahlxlihao@163.com
 *
 */
@Controller
@RequestMapping("/backstage/category")
public class CategoryController extends BaseController{

	@Autowired
	private CategoryService categoryService;

	/**
	 * 跳转产品分类列表页面
	 * @return
	 * @throws IOException 
	 */
	@RequestMapping(value = "/list.html", method = { RequestMethod.GET })
	public ModelAndView list(CategoryBean category) throws IOException{
		ModelAndView mv=this.backstageModelAndView("category/list");
		return mv;

	}
	
	/**
	 * 查询产品分类
	 * @return
	 * @throws IOException 
	 */
	@RequestMapping(value = "/list.do", method = { RequestMethod.POST })
	@ResponseBody
	public ResultBean<CategoryBean> do_list(CategoryBean category) throws IOException{
		ResultBean<CategoryBean> result = new ResultBean<CategoryBean>();
		List<CategoryBean> categoryList = categoryService.list(category);
		//category总记录数
		int records=categoryService.queryCount(category);
		result.setRows(categoryList);
		result.setPage(category.getPage());
		result.setSize(category.getRows());
		result.setRecords(records);
		return result;

	}
	
	/**
	 * 添加产品分类
	 * @return
	 */
	@RequestMapping(value = "/add.do", method = { RequestMethod.POST })
	@ResponseBody
	public Map<String, Object> add(CategoryBean category){
		Map<String, Object> result = new HashMap<String, Object>();
		category=categoryService.save(category);
		result.put("category", category);
		return result;

	}
	
	/**
	 * 删除产品分类
	 * @return
	 */
	@RequestMapping(value = "/delete.do", method = { RequestMethod.POST })
	@ResponseBody
	public Map<String, Object>  delete(CategoryBean category){
		Map<String, Object> result = new HashMap<String, Object>();
		boolean flag=categoryService.delete(category);
		result.put("isSuccess", flag);
		result.put("category", category);
		return result;

	}
	
	/**
	 * 添加产品分类与属性关系
	 * @return
	 */
	@RequestMapping(value = "/addCategoryRelAttr.do", method = { RequestMethod.POST })
	@ResponseBody
	public Map<String, Object> addCategoryRelAttr(CategoryRelAttrBean categoryRelAttr){
		Map<String, Object> result = new HashMap<String, Object>();
		boolean flag=categoryService.saveCategoryRelAttr(categoryRelAttr);
		result.put("isSuccess", flag);
		result.put("categoryRelAttr", categoryRelAttr);
		return result;

	}
	

}
